package com.rtwo.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by hasee on 2017/5/4.
 */
public class SQLAction {

    public static void exeSQL(String user,String passwd,String jdbcUrl,String driver,String sql) {

        try {
            //通过Map方式设置Druid参数
            Map<String, String> druidMap=new HashMap<String, String>();
            druidMap.put(DruidDataSourceFactory.PROP_USERNAME, user);
            druidMap.put(DruidDataSourceFactory.PROP_PASSWORD, passwd);
            druidMap.put(DruidDataSourceFactory.PROP_URL, jdbcUrl);
            druidMap.put(DruidDataSourceFactory.PROP_DRIVERCLASSNAME, driver);
            //通过DruidDataSourceFactory获取DataSource实例
            DataSource dataSource=DruidDataSourceFactory.createDataSource(druidMap);
            Connection conn=dataSource.getConnection();
            Statement st=conn.createStatement();
            ResultSet result=st.executeQuery(sql);
            while(result.next()) {
                System.out.println("Name:" + result.getString("NAME"));
            }
            result.close();
            st.close();
            conn.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}
